<?php
/* SVN FILE: $Id: users_controller.php 36 2009-10-22 21:25:27Z kenreily@gmail.com $ */
/**
 * Short description for file.
 *
 * Long description for file
 *
 * PHP versions 4 and 5
 *
 * Recenseo: Online submission and reviewing for education
 * Copyright 2008-2009, Ken Reily
 *
 * Licensed under The MIT License
 * Redistributions of files must retain the above copyright notice.
 *
 * $Date: 2009-10-22 21:25:27 +0000 (Thu, 22 Oct 2009) $
 * $Revision: 36 $
 * $Author: kenreily@gmail.com $
 * $HeadURL: http://recenseo.googlecode.com/svn/trunk/app/controllers/users_controller.php $
 */
class UsersController extends AppController
{
  var $name = 'Users';
  var $scaffold = 'admin';
  var $uses = array('User','Assessment','AssessmentTestCase','Course','UserStatsImpression');
  
  function logout()
  {
    $this->redirect($this->Auth->logout());
  }

  // Calculates the percentile of a value within the greater array
  private function percentile($index,$key,$array)
  {
    $target_val = $array[$index]['UserStatistic'][$key];

    if (!empty($target_val))
    {
      $num_total = 0;
      $num_less = 0;
      
      foreach ($array as $stat)
      {
	if (!empty($stat['UserStatistic'][$key]))
	{
	  $num_total++;
	  
	  if ($key == 'responsiveness' || $key == 'abs_accuracy')
	  {
	    if ($stat['UserStatistic'][$key] >= $target_val)
	    {
	      $num_less++;
	    }
	  }
	  else if ($stat['UserStatistic'][$key] <= $target_val)
	  {
	    $num_less++;
	  }
	}
      }
    }
    
    if (empty($num_total) || empty($target_val))
    {
      return null;
    }
    else
    {
      return $num_less / $num_total;
    }
  }
  
  function performance($course_id)
  {
    $user = $this->Session->read('Auth.User');
    
    // Rating type 1 = leaderboard
    if ($user['rating_type'] == 1 || $user['role'] == 'Instructor')
    {
      $data = $this->User->getLeaderboardData($course_id);
      $stat_keys = array('avg_test_cases',
			 'avg_feedback_chars',
			 'author_feedback_score',
			 'peer_feedback_score',
			 'responsiveness',
			 'abs_accuracy');

      // Prime the ref array with initial values.
      $ref = array();
      foreach ($stat_keys as $stat_key)
      {
        if ($stat_key == 'abs_accuracy')
	{
	  $ref[$stat_key] = $data[0][0][$stat_key];
	}
	else
	{
	  $ref[$stat_key] = $data[0]['UserStatistic'][$stat_key];
	}
      }

      // Move the absolute value column to the main UserStatistic key 
      // for consistency.
      foreach ($data as $index => $stat)
      {
	$data[$index]['UserStatistic']['abs_accuracy'] = 
	  $data[$index][0]['abs_accuracy'];
      }

      foreach ($data as $index => $stat)
      {
	foreach ($stat['UserStatistic'] as $key => $value)
	{
	  if ($key == 'responsiveness')
	  {
	    if (!empty($value) && $value < $ref['responsiveness'])
	    {
	      $ref['responsiveness'] = $value;
	    }
	  }
	  else if ($key == 'abs_accuracy' && !empty($value) && $value < $ref['abs_accuracy'])
	  {
	    $ref['abs_accuracy'] = $value;
	    $ref['accuracy'] = $stat['UserStatistic']['accuracy'];
	  }
	  else if (array_key_exists($key,$ref) && !empty($value) && $value > $ref[$key])
	  {
	    $ref[$key] = $value;
	  }

	  if ($key != 'accuracy')
	  {
	    $percentile = $this->percentile($index,$key,$data);

	    if (!empty($percentile))
	    {
	      $data[$index]['Percentile'][$key] = $percentile;
	    }
	  }
	}

	if (array_key_exists('Percentile',$data[$index]))
	{
	  $data[$index]['Percentile']['total'] = 
	    array_sum($data[$index]['Percentile']) / count($data[$index]['Percentile']);
	}
      }

      function cmpTotals($a, $b)
      {
	if (!array_key_exists('Percentile',$a))
	{
	  return 1;
	}
	else if (!array_key_exists('Percentile',$b))
	{
	  return -1;
	}
	else if ($a['Percentile']['total'] == $b['Percentile']['total'])
	{
	  return 0;
	}
	else
	{
	  return ($a['Percentile']['total'] > $b['Percentile']['total']) ? -1 : 1;
	}
      }

      uasort($data,'cmpTotals');

      $this->set('data',$data);
      $this->set('ref',$ref);
    }
    else if ($user['rating_type'] == 2)
    {
      $data['user'] = $this->User->getUserStats($user['username']);
      $data['ta'] = $this->User->getUserStats('sing0389');
      $this->set('data',$data);
    }
    else
    {
      $this->Session->setFlash('Performance report not available.');
    }

    $this->pageTitle = 'Reviewer Performance Report';

    $impression = array('UserStatsImpression' => array('user_id' => $user['id']));
    $this->UserStatsImpression->save($impression);
  }
}
?>